package com.aj.controller;


import com.aj.Interface.OrderService;
import com.aj.common.MessageConstant;
import com.aj.pojo.CheckItem;
import com.aj.utils.SMSUtils;
import com.alibaba.dubbo.config.annotation.Reference;
import entity.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

import java.util.Map;

@RequestMapping("/order")
@RestController
public class OrderController {

    @Reference
    private OrderService orderService;

    @Autowired
    private JedisPool jedisPool;

    @RequestMapping("/findById")
    public Result findById(Integer id) {
        try {
            Map map = orderService.findById(id);
            return new Result(true, MessageConstant.ORDER_SUCCESS, map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ORDER_FALSE);
        }
    }

    @RequestMapping("/submit")
    public Result submit(@RequestBody Map map) {
        try {
            String telephone = (String) map.get("telephone");
            String jediscode = jedisPool.getResource().get(telephone);
            if (jediscode == null && !telephone.equals(jediscode)) {
                return new Result(false, MessageConstant.VALIDATECODE_ERROR);
            }

            Result result = orderService.submit(map);
            if (result.isFlag()){
                SMSUtils.sendShortMessage(SMSUtils.ORDER_NOTICE, telephone, (String) map.get("orderDate"));
            }

                return result;
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ORDER_FALSE);
        }
    }
}
